<?php

class Blog extends CI_Controller
{
	function __construct()
	{
		parent::__construct();

		$this->result = '我是在构造函数里面赋值的哦';
	}

	public function index($name='')
	{
		$this->benchmark->mark('start');
		echo '您好!'.$name;

		//来一个循环
		for($i = 0; $i<100; $i++)
		{
			echo $i;
		}

		$this->benchmark->mark('end');

		echo $this->benchmark->elapsed_time('start','end');
	}

	/*
	function _remap($method)
	{
		if($method == 'index')
		{
			echo '这个是index方法';
			$this->$method();
		}
		else
		{
			echo '这个是其他方法';
			$this->$method();
		}
	}

	function _output()
	{
		echo '总是有这个尾巴！ '. $this->result;
	}
	*/

	function _private()
	{
		echo '我是私有方法，你访问不到我';
	}

	//view
	function blogview()
	{
		$data = array(
			'title' => 'My Title',
			'heading' => 'My Heading',
			'message' => array('Clean House', 'Call Mom', 'Run Errands')
		);

		$buffer = $this->load->view('blogview',$data,true);

		echo $buffer."这个是取出来后再输出的";
	}

	//model
	function blogmodel()
	{
		$config['hostname'] = "localhost";
		$config['username'] = "root";
		$config['password'] = "";
		$config['database'] = "ci";
		$config['dbdriver'] = "mysql";
		$config['dbprefix'] = "";
		$config['pconnect'] = FALSE;
		$config['db_debug'] = TRUE;


		$this->load->model('Blogmodel','blog',$config);

		$data['query'] = $this->blog->get_last_ten_entries(); 

		$this->load->view('blogmodel',$data);

		$this->output->enable_profiler(TRUE); //如果开启缓存的话 不会显示

		//$this->output->cache(1); //缓存1分钟看看
	}

	//加载辅助函数
	function loadhelper()
	{
		$this->load->helper('url');

		$this->load->view('loadhelper');
	}

	//扩展辅助函数
	function newarray()
	{
		$this->load->helper('array');

		$data = array(
			'title' => 'My Title',
			'heading' => 'My Heading',
			'message' => array('Clean House', 'Call Mom', 'Run Errands')
		);

		echo random_element($data).'<br>';

		echo new_in_array();
	}

	//自己写的类库
	function myclass()
	{
		$params = array('type' => 'large', 'color' => 'red');

		$this->load->library('myclass',$params);

		$this->myclass->first();

		$this->myclass->second();
	}

	function show_error()
	{
		show_error('这个是错误信息！',500);
	}

	function error_404()
	{
		$this->load->helper('url');

		show_404(current_url());
	}

	function show_log()
	{
		log_message('error','这是一个致命错误呀！！！');
	}

	//日历
	function show_calendar()
	{
		$this->load->helper('url');
		$prefs = array (
               'start_day'    => 'saturday',
               'month_type'   => 'long',
               'day_type'     => 'short',
			   'show_next_prev' => TRUE,
			   'next_prev_url' => base_url().$this->router->fetch_class().'/'.$this->router->fetch_method()
             );

		$this->load->library('calendar',$prefs);
		$data = array(
               3  => 'http://www.baidu.com',
               7  => 'http://example.com/news/article/2006/07/',
               13 => 'http://example.com/news/article/2006/13/',
               26 => 'http://example.com/news/article/2006/26/'
             );
		echo $this->calendar->generate($this->uri->segment(3), $this->uri->segment(4),$data);

		//$this->output->enable_profiler(TRUE);
	}

	//session
	function my_session()
	{
		$this->load->library('session');
	}

	//购物车
	function my_cart_insert()
	{
		$this->load->library('cart');
		$data = array(
               array(
                       'id'      => 'sku_123ABC',
                       'qty'     => 1,
                       'price'   => 39.95,
                       'name'    => 'T-Shirt',
                       'options' => array('Size' => 'L', 'Color' => 'Red')
                    ),
               array(
                       'id'      => 'sku_567ZYX',
                       'qty'     => 1,
                       'price'   => 9.95,
                       'name'    => 'Coffee Mug'
                    ),
               array(
                       'id'      => 'sku_965QRS',
                       'qty'     => 1,
                       'price'   => 29.95,
                       'name'    => 'Shot Glass'
                    )
            );

		$this->cart->insert($data); 

	}

	function my_cart_view()
	{
		$this->load->helper('form');
		$this->load->library('cart');
		$this->load->view('my_cart_view');
	}

	function my_cart_update()
	{
		$this->load->library('cart');

		foreach($_POST as $val)
		{
			$data[] = $val;
		}

		$this->cart->update($data);
	}

}